package com.imooc.hadoop.mapreduce.MultipleInputs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

/**
 * @program: hadoop
 * @description: MultipleInputsJob
 * @author: zengchen
 * @create: 2021-04-01 08:51
 **/
public class MultipleInputsJob {
    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            System.exit(100);
        }
        Configuration conf = new Configuration();
        //解析命令行中通过-D传递过来的参数，添加到conf中
        String[] remainingArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

        Job job = Job.getInstance(conf);
        job.setJarByClass(MultipleInputsJob.class);

        MultipleInputs.addInputPath(job, new Path(remainingArgs[0]), TextInputFormat.class, AbcMapper.class);
        MultipleInputs.addInputPath(job, new Path(remainingArgs[1]), TextInputFormat.class, XyzMapper.class);
        FileOutputFormat.setOutputPath(job, new Path(remainingArgs[2]));

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(MultipleInputReducer.class);
        job.setOutputValueClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        job.waitForCompletion(true);

    }
}
